package com.joysuch.wwyt.api.basedata.service.impl;

import cn.zlg.common.util.CollectionUtils;
import com.joysuch.wwyt.api.basedata.bean.BpJobSyncAddBean;
import com.joysuch.wwyt.api.basedata.bean.BpJobSyncBean;
import com.joysuch.wwyt.api.basedata.bean.BpJobSyncRequestBean;
import com.joysuch.wwyt.api.basedata.service.BpJobSyncService;
import com.joysuch.wwyt.bp.entity.BpJob;
import com.joysuch.wwyt.core.entity.BaseConfig;
import com.joysuch.wwyt.core.enums.BaseConfigCodes;
import com.joysuch.wwyt.core.service.BaseConfigService;
import com.joysuch.wwyt.dzhapi.bean.ApiBaseConfigBean;
import com.joysuch.wwyt.dzhapi.dto.BuildDataDto;
import com.joysuch.wwyt.dzhapi.mapper.ApiBaseConfigMapper;
import com.joysuch.wwyt.dzhapi.mapper.ApiBpJobMapper;
import com.joysuch.wwyt.monitoringcenter.bean.LocateLoginRespDto;
import com.joysuch.wwyt.monitoringcenter.service.LocateServerService;
import com.joysuch.wwyt.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.info.ProjectInfoProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class BpJobSyncServiceImpl implements BpJobSyncService {


    @Autowired
    private BaseConfigService baseConfigService;

    @Autowired
    private LocateServerService locateServerService;
    @Autowired
    private ApiBaseConfigMapper baseConfigMapper;

    @Override
    public boolean addSync(BpJob entity) {
        log.info("岗位新增同步开始........");
        boolean flag = false;
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkIfSync(entity.getTenentId(), entity.getOrgCode().substring(0, 7))) {
            return false;
        }
        BpJobSyncRequestBean requestBean = new BpJobSyncRequestBean();
        BaseConfig buildConfig = baseConfigService.findConfig(entity.getTenentId(), entity.getOrgCode().substring(0, 7), BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("岗位新增同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        List<BpJobSyncAddBean> beans = new ArrayList<>();
        BpJobSyncAddBean bean = new BpJobSyncAddBean();
        bean.setName(entity.getName());
        bean.setUniqueId(entity.getUniqueId());
        bean.setDescription(entity.getDescription());
        bean.setPersonTemplateId(entity.getAlarmTemplateId());
        beans.add(bean);

        requestBean.setBuildId(buildConfig.getValue());
        requestBean.setData(beans);

        try {
            ResponseEntity<LocateLoginRespDto> response = locateServerService.restPost(requestBean, BaseConfigCodes.LOCATE_API_SYNC_JOB_ADD, LocateLoginRespDto.class);
            if (response == null) {
                log.error("岗位新增同步异常： response is null");
            } else {
                if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                    log.error("岗位新增同步异常:name= " + entity.getName() + "--{" + response.getBody().getErrMsg() + "}");
                } else {
                    if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                        log.info("岗位新增同步成功 name: " + entity.getName());
                        flag = true;
                    } else {
                        log.error("岗位新增同步异常:name= " + entity.getName());
                    }
                }
            }
        } catch (Exception e) {
            log.error("岗位新增同步异常 e{}", e);
        }
        log.info("岗位新增同步结束........");
        return flag;
    }

    @Override
    public boolean addApiSync(BpJob entity, BuildDataDto dto) {
        log.info("岗位新增同步开始........");
        boolean flag = false;
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkApiIfSync(dto)) {
            return false;
        }
        BpJobSyncRequestBean requestBean = new BpJobSyncRequestBean();
        ApiBaseConfigBean buildConfig = baseConfigMapper.findConfig(dto.getTenentId(), dto.getOrgCode(), BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("岗位新增同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        List<BpJobSyncAddBean> beans = new ArrayList<>();
        BpJobSyncAddBean bean = new BpJobSyncAddBean();
        bean.setName(entity.getName());
        bean.setUniqueId(entity.getUniqueId());
        bean.setDescription(entity.getDescription());
        bean.setPersonTemplateId(entity.getAlarmTemplateId());
        beans.add(bean);

        requestBean.setBuildId(buildConfig.getValue());
        requestBean.setData(beans);

        try {
            ResponseEntity<LocateLoginRespDto> response = locateServerService.apiRestPost(requestBean, BaseConfigCodes.LOCATE_API_SYNC_JOB_ADD, LocateLoginRespDto.class, dto);
            if (response == null) {
                log.error("岗位新增同步异常： response is null");
            } else {
                if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                    log.error("岗位新增同步异常:name= " + entity.getName() + "--{" + response.getBody().getErrMsg() + "}");
                } else {
                    if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                        log.info("岗位新增同步成功 name: " + entity.getName());
                        flag = true;
                    } else {
                        log.error("岗位新增同步异常:name= " + entity.getName());
                    }
                }
            }
        } catch (Exception e) {
            log.error("岗位新增同步异常 e{}", e);
        }
        log.info("岗位新增同步结束........");
        return flag;
    }

    @Override
    public boolean updateSync(BpJob entity) {
        log.info("岗位修改同步开始........");
        boolean flag = false;
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkIfSync(entity.getTenentId(), entity.getOrgCode().substring(0, 7))) {
            return false;
        }
        BaseConfig buildConfig = baseConfigService.findFirstConfig(BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("岗位修改同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }

        BpJobSyncBean bean = new BpJobSyncBean();
        bean.setName(entity.getName());
        bean.setUniqueId(entity.getUniqueId());
        bean.setDescription(entity.getDescription());
        bean.setPersonTemplateId(entity.getAlarmTemplateId());
        bean.setBuildId(buildConfig.getValue());
        try {
            ResponseEntity<LocateLoginRespDto> response = locateServerService.restPost(bean, BaseConfigCodes.LOCATE_API_SYNC_JOB_UPDATE, LocateLoginRespDto.class);
            if (response == null) {
                log.error("岗位修改同步异常：response is null");
            } else {
                if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                    log.error("岗位修改同步异常:name= " + entity.getName() + "--{" + response.getBody().getErrMsg() + "}");
                } else {
                    if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                        log.info("岗位修改同步成功 name: " + entity.getName());
                        flag = true;
                    } else {
                        log.error("岗位修改同步异常:name= " + entity.getName());
                    }
                }
            }
        } catch (Exception e) {
            log.error("岗位修改同步异常 e{}", e);
        }
        log.info("岗位修改同步结束........");
        return flag;
    }


    @Override
    public boolean apiUpdateSync(BpJob entity, BuildDataDto dto) {
        log.info("岗位修改同步开始........");
        boolean flag = false;
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkApiIfSync(dto)) {
            return false;
        }
        ApiBaseConfigBean buildConfig = baseConfigMapper.findConfig(dto.getTenentId(), dto.getOrgCode(), BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("岗位修改同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }

        BpJobSyncBean bean = new BpJobSyncBean();
        bean.setName(entity.getName());
        bean.setUniqueId(entity.getUniqueId());
        bean.setDescription(entity.getDescription());
        bean.setPersonTemplateId(entity.getAlarmTemplateId());
        bean.setBuildId(buildConfig.getValue());
        try {
            ResponseEntity<LocateLoginRespDto> response = locateServerService.apiRestPost(bean, BaseConfigCodes.LOCATE_API_SYNC_JOB_UPDATE, LocateLoginRespDto.class, dto);
            if (response == null) {
                log.error("岗位修改同步异常：response is null");
            } else {
                if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                    log.error("岗位修改同步异常:name= " + entity.getName() + "--{" + response.getBody().getErrMsg() + "}");
                } else {
                    if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                        log.info("岗位修改同步成功 name: " + entity.getName());
                        flag = true;
                    } else {
                        log.error("岗位修改同步异常:name= " + entity.getName());
                    }
                }
            }
        } catch (Exception e) {
            log.error("岗位修改同步异常 e{}", e);
        }
        log.info("岗位修改同步结束........");
        return flag;
    }

    @Override
    public boolean deleteSync(String uniqueId) {
        // 检查是否需要同步： 如果无需同步直接返回
        log.info("岗位删除同步开始........");
        boolean flag = false;
        if (!baseConfigService.checkIfSync()) {
            return false;
        }
        BaseConfig buildConfig = baseConfigService.findConfig(BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("岗位删除同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        BpJobSyncBean bean = new BpJobSyncBean();
        bean.setUniqueId(uniqueId);
        bean.setBuildId(buildConfig.getValue());
        ResponseEntity<LocateLoginRespDto> response = null;
        try {
            response = locateServerService.restPost(bean, BaseConfigCodes.LOCATE_API_SYNC_JOB_DELETE, LocateLoginRespDto.class);
        } catch (Exception e) {
            log.error("岗位删除同步异常 e{}", e);
        }
        if (response == null) {
            log.error("岗位删除同步异常：response is null");
        } else {
            if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                log.info("岗位删除同步成功 uniqueId: " + uniqueId);
                flag = true;
            } else {
                log.info("岗位删除同步失败 uniqueId: " + uniqueId);
            }
        }
        log.info("岗位删除同步结束........");
        return flag;
    }

    @Override
    public boolean deleteApiSync(String uniqueId, BuildDataDto dto) {
        // 检查是否需要同步： 如果无需同步直接返回
        log.info("岗位删除同步开始........");
        boolean flag = false;
        if (!baseConfigService.checkApiIfSync(dto)) {
            return false;
        }
        ApiBaseConfigBean buildConfig = baseConfigMapper.findConfig(dto.getTenentId(), dto.getOrgCode(), BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("岗位删除同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        BpJobSyncBean bean = new BpJobSyncBean();
        bean.setUniqueId(uniqueId);
        bean.setBuildId(buildConfig.getValue());
        ResponseEntity<LocateLoginRespDto> response = null;
        try {
            response = locateServerService.apiRestPost(bean, BaseConfigCodes.LOCATE_API_SYNC_JOB_DELETE, LocateLoginRespDto.class, dto);
        } catch (Exception e) {
            log.error("岗位删除同步异常 e{}", e);
        }
        if (response == null) {
            log.error("岗位删除同步异常：response is null");
        } else {
            if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                log.info("岗位删除同步成功 uniqueId: " + uniqueId);
                flag = true;
            } else {
                log.info("岗位删除同步失败 uniqueId: " + uniqueId);
            }
        }
        log.info("岗位删除同步结束........");
        return flag;
    }

    @Override
    public boolean deleteSyncCross(String uniqueId, Long tenentId, String orgCode, String buildId) {
        // 检查是否需要同步： 如果无需同步直接返回
        log.info("岗位删除同步开始........");
        boolean flag = false;
        if (!baseConfigService.checkIfSync()) {
            return false;
        }
//        BaseConfig buildConfig = baseConfigService.findConfig(BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (StringUtil.isEmpty(buildId)) {
            log.info("岗位删除同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        BpJobSyncBean bean = new BpJobSyncBean();
        bean.setUniqueId(uniqueId);
        bean.setBuildId(buildId);
        ResponseEntity<LocateLoginRespDto> response = locateServerService.restPostCross(bean, BaseConfigCodes.LOCATE_API_SYNC_JOB_DELETE, LocateLoginRespDto.class, tenentId, orgCode, buildId);
        if (response == null) {
            log.error("岗位删除同步异常：response is null");
        } else {
            if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                log.info("岗位删除同步成功 uniqueId: " + uniqueId);
                flag = true;
            } else {
                log.info("岗位删除同步失败 uniqueId: " + uniqueId);
            }
        }
        log.info("岗位删除同步结束........");
        return flag;
    }

    //适用版本：化工4.1及以上版本
    @Override
    public Map<String, String> syncV2ListName() {
        Map<String, String> jobMap = new LinkedHashMap<>();
        if (!baseConfigService.checkIfSync()) {
            return jobMap;
        }
        BaseConfig buildConfig = baseConfigService.findFirstConfig(BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("岗位列表获取异常： 人员在岗在位buildId 未配置。");
            return jobMap;
        }
        BpJobSyncBean bean = new BpJobSyncBean();
        bean.setBuildId(buildConfig.getValue());
        ResponseEntity<LocateLoginRespDto> response = locateServerService.restPost(bean, BaseConfigCodes.LOCATE_API_SYNC_JOB_LIST, LocateLoginRespDto.class);
        if (response == null) {
            log.error("岗位列表获取异常同步异常： response is null");
        } else {
            LocateLoginRespDto dto = response.getBody();
            if (StringUtils.isNotBlank(dto.getErrMsg())) {
                log.info("岗位列表获取异常： {}", dto.getErrMsg());
            }
            if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                List<Map<String, String>> records = (List<Map<String, String>>) dto.getData();
                if (CollectionUtils.isNotEmpty(records)) {
                    for (Map<String, String> map : records) {
                        Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
                        while (iterator.hasNext()) {
                            //岗位名称唯一 将名称作为key 人员定位的id作为value
                            Map.Entry<String, String> entry = iterator.next();
                            jobMap.put(entry.getValue(), entry.getKey());
                        }
                    }
                } else {
                    log.info("岗位列表为空....");
                }
            }
        }
        return jobMap;
    }

    @Override
    public boolean syncjob(BpJob entity, Map<String, String> jobMap) {
        boolean result = false;
        if (!jobMap.isEmpty()) {
            if (jobMap.get(entity.getName()) != null) {
                result = this.updateSync(entity);
            } else {
                result = this.addSync(entity);
            }
        } else {

        }
        return result;
    }

    @Override
    public boolean syncjob(BpJob entity) {
        boolean result = false;
        boolean b = this.getSyncJob(entity);
        if (b) {
            result = this.updateSync(entity);
        } else {
            result = this.addSync(entity);
        }
        return result;
    }

    @Override
    public boolean syncApiJob(BpJob entity, BuildDataDto buildDataDto) {
        boolean result = false;
        boolean b = this.getApiSyncJob(entity, buildDataDto);
        if (b) {
            result = this.apiUpdateSync(entity, buildDataDto);
        } else {
            result = this.addApiSync(entity, buildDataDto);
        }
        return result;
    }

    //跨厂岗位同步
    @Override
    public boolean syncjobCross(BpJob entity, String buildId, Long tenentId, String orgCode) {
        boolean result = false;
        boolean b = this.getSyncJobCross(entity, buildId, tenentId, orgCode);
        if (b) {
            result = this.updateSyncCross(entity, buildId, tenentId, orgCode);
        } else {
            result = this.addSyncCross(entity, buildId, tenentId, orgCode);
        }
        return result;
    }

    public boolean addSyncCross(BpJob entity, String buildId, Long tenentId, String orgCode) {
        log.info("岗位新增同步开始........");
        boolean flag = false;
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkIfSync()) {
            return false;
        }
        BpJobSyncRequestBean requestBean = new BpJobSyncRequestBean();
//        BaseConfig buildConfig = baseConfigService.findConfig(entity.getTenentId(), entity.getOrgCode().substring(0, 7), BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (StringUtil.isEmpty(buildId)) {
            log.info("岗位新增同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        List<BpJobSyncAddBean> beans = new ArrayList<>();
        BpJobSyncAddBean bean = new BpJobSyncAddBean();
        bean.setName(entity.getName());
        bean.setUniqueId(entity.getUniqueId());
        bean.setDescription(entity.getDescription());
        bean.setPersonTemplateId(entity.getAlarmTemplateId());
        beans.add(bean);

        requestBean.setBuildId(buildId);
        requestBean.setData(beans);

        ResponseEntity<LocateLoginRespDto> response = locateServerService.restPostCross(requestBean, BaseConfigCodes.LOCATE_API_SYNC_JOB_ADD, LocateLoginRespDto.class, tenentId, orgCode, buildId);
        if (response == null) {
            log.error("岗位新增同步异常： response is null");
        } else {
            if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                log.error("岗位新增同步异常:name= " + entity.getName() + "--{" + response.getBody().getErrMsg() + "}");
            } else {
                if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                    log.info("岗位新增同步成功 name: " + entity.getName());
                    flag = true;
                } else {
                    log.error("岗位新增同步异常:name= " + entity.getName());
                }
            }
        }
        log.info("岗位新增同步结束........");
        return flag;
    }

    public boolean updateSyncCross(BpJob entity, String buildId, Long tenentId, String orgCode) {
        log.info("岗位修改同步开始........");
        boolean flag = false;
        // 检查是否需要同步： 如果无需同步直接返回
        if (!baseConfigService.checkIfSync()) {
            return false;
        }
//        BaseConfig buildConfig = baseConfigService.findFirstConfig(BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (StringUtil.isEmpty(buildId)) {
            log.info("岗位修改同步异常： 人员在岗在位buildId 未配置。");
            return false;
        }

        BpJobSyncBean bean = new BpJobSyncBean();
        bean.setName(entity.getName());
        bean.setUniqueId(entity.getUniqueId());
        bean.setDescription(entity.getDescription());
        bean.setPersonTemplateId(entity.getAlarmTemplateId());
        bean.setBuildId(buildId);
        ResponseEntity<LocateLoginRespDto> response = locateServerService.restPostCross(bean, BaseConfigCodes.LOCATE_API_SYNC_JOB_UPDATE, LocateLoginRespDto.class, tenentId, orgCode, buildId);
        if (response == null) {
            log.error("岗位修改同步异常：response is null");
        } else {
            if (StringUtils.isNotBlank(response.getBody().getErrMsg())) {
                log.error("岗位修改同步异常:name= " + entity.getName() + "--{" + response.getBody().getErrMsg() + "}");
            } else {
                if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK) {
                    log.info("岗位修改同步成功 name: " + entity.getName());
                    flag = true;
                } else {
                    log.error("岗位修改同步异常:name= " + entity.getName());
                }
            }
        }
        log.info("岗位修改同步结束........");
        return flag;
    }

    /**
     * 查询 Job
     *
     * @return
     */
    @Override
    public boolean getSyncJob(BpJob job) {
        log.info("岗位同步查询 Start...UniqueId :" + job.getUniqueId());
        boolean flag = false;
        if (!baseConfigService.checkIfSync()) {
            return false;
        }
        BaseConfig buildConfig = baseConfigService.findFirstConfig(BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("岗位同步查询异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        BpJobSyncBean bean = new BpJobSyncBean();
        bean.setBuildId(buildConfig.getValue());
        bean.setUniqueId(job.getUniqueId());
        try {
            ResponseEntity<LocateLoginRespDto> response = locateServerService.restPost(bean, BaseConfigCodes.LOCATE_API_SYNC_JOB_SEARCH, LocateLoginRespDto.class);
            if (response == null) {
                log.error("岗位同步查询异常： response is null");
            } else {
                LocateLoginRespDto dto = response.getBody();
                if (StringUtils.isNotBlank(dto.getErrMsg())) {
                    log.info("岗位同步查询异常： {}", dto.getErrMsg());
                } else {
                    if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK && dto.getData() != null) {
                        flag = true;
                    } else {
                        log.info("岗位同步查询异常： {}", dto.getErrMsg());
                    }
                }
            }
        } catch (Exception e) {
            log.error("岗位同步查询异常 e{}", e);
        }
        log.info("岗位同步查询 End...UniqueId: " + job.getUniqueId());
        return flag;
    }

    public boolean getApiSyncJob(BpJob job, BuildDataDto dto) {
        log.info("岗位同步查询 Start...UniqueId :" + job.getUniqueId());
        boolean flag = false;
        if (!baseConfigService.checkApiIfSync(dto)) {
            return false;
        }
        ApiBaseConfigBean buildConfig = baseConfigMapper.findConfig(dto.getTenentId(), dto.getOrgCode(), BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (buildConfig == null) {
            log.info("岗位同步查询异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        BpJobSyncBean bean = new BpJobSyncBean();
        bean.setBuildId(buildConfig.getValue());
        bean.setUniqueId(job.getUniqueId());
        try {
            ResponseEntity<LocateLoginRespDto> response = locateServerService.apiRestPost(bean, BaseConfigCodes.LOCATE_API_SYNC_JOB_SEARCH, LocateLoginRespDto.class, dto);
            if (response == null) {
                log.error("岗位同步查询异常： response is null");
            } else {
                LocateLoginRespDto respDtodto = response.getBody();
                if (StringUtils.isNotBlank(respDtodto.getErrMsg())) {
                    log.info("岗位同步查询异常： {}", respDtodto.getErrMsg());
                } else {
                    if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK && respDtodto.getData() != null) {
                        flag = true;
                    } else {
                        log.info("岗位同步查询异常： {}", respDtodto.getErrMsg());
                    }
                }
            }
        } catch (Exception e) {
            log.error("岗位同步查询异常 e{}", e);
        }
        log.info("岗位同步查询 End...UniqueId: " + job.getUniqueId());
        return flag;
    }

    /**
     * 查询 Job
     *
     * @return
     */
    public boolean getSyncJobCross(BpJob job, String buildId, Long tenetId, String orgCode) {
        log.info("岗位同步查询 Start...UniqueId :" + job.getUniqueId());
        boolean flag = false;
        if (!baseConfigService.checkIfSync()) {
            return false;
        }
//        BaseConfig buildConfig = baseConfigService.findFirstConfig(BaseConfigCodes.LOCATE_CURRENT_BUILD_ID);
        if (StringUtil.isEmpty(buildId)) {
            log.info("岗位同步查询异常： 人员在岗在位buildId 未配置。");
            return false;
        }
        BpJobSyncBean bean = new BpJobSyncBean();
        bean.setBuildId(buildId);
        bean.setUniqueId(job.getUniqueId());
        ResponseEntity<LocateLoginRespDto> response = locateServerService.restPostCross(bean, BaseConfigCodes.LOCATE_API_SYNC_JOB_SEARCH, LocateLoginRespDto.class, tenetId, orgCode, buildId);
        if (response == null) {
            log.error("岗位同步查询异常： response is null");
        } else {
            LocateLoginRespDto dto = response.getBody();
            if (StringUtils.isNotBlank(dto.getErrMsg())) {
                log.info("岗位同步查询异常： {}", dto.getErrMsg());
            } else {
                if (response.getStatusCode() != null && response.getStatusCode() == HttpStatus.OK && dto.getData() != null) {
                    flag = true;
                } else {
                    log.info("岗位同步查询异常： {}", dto.getErrMsg());
                }
            }
        }
        log.info("岗位同步查询 End...UniqueId: " + job.getUniqueId());
        return flag;
    }
}
